﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Asteroids.Game
{
    public class Ship : EntityBase
    {
        //Need to replace Float it was just a place holding Type
        public float Team { get; set; }
        public Weapon CurrentWeapon { get; set; }
        public bool[] InputStatus { get; set; }
        public int Shield { get; set; }
        private const float AccelerationFactor = 0.2F;

        public Ship(Coordinate location)
        {
            Shield = 0;
            Radius = 10;
            Location = location;
            CurrentWeapon = new Weapon(Location);

            FaceDirection = Velocity.Angle;

            VertexData = new List<PolarCoordinate>
                                 {
                                     new PolarCoordinate(0, 12.0f),
                                     new PolarCoordinate((float) Math.PI*2/3, 10.0f),
                                     new PolarCoordinate(0.0f, 0.0f),
                                     new PolarCoordinate((float) Math.PI*4/3, 10.0f)
                                 };
        }

        public override void Update(UpdateEventArgs e)
        {
            //Up
            if (InputStatus[0])
            {
                Velocity += new PolarCoordinate(FaceDirection, AccelerationFactor);
            }
            //Down
            if (InputStatus[1])
                Velocity *= 1 - AccelerationFactor;
            // drag
            if (!InputStatus[0] && !InputStatus[1])
                Velocity *= 0.99;
            //Right    
            if (InputStatus[2])
                FaceDirection += Math.PI / 25;
            //Left
            if (InputStatus[3])
                FaceDirection -= Math.PI / 25;
            //Shoot weapon
            if (InputStatus[4])
                CurrentWeapon.Fire(FaceDirection);

            base.Update(e);

            UpdateWeapon(e);
        }

        public override Canvas Render(RenderEventArgs e)
        {
            var retCanvas = new Canvas();
            if (VertexData.Count != 0 && !Dead)
            {
                retCanvas.Children.Add(base.Render(e));

                var tempList = new List<Line>();

                for (int j = 1; j <= Shield; j++ )
                {
                    double inc = 2*Math.PI/30;
                    for (double i = 0; i < Math.PI * 2; i += inc)
                    {
                        tempList.Add(new Line()
                        {
                            X1 = Location.X + (Math.Cos(i) * (Radius + 5 + 2 * j)),
                            Y1 = Location.Y + (Math.Sin(i) * (Radius + 5 + 2 * j)),
                            X2 = Location.X + (Math.Cos(i + inc) * (Radius + 5 + 2 * j)),
                            Y2 = Location.Y + (Math.Sin(i + inc) * (Radius + 5 + 2 * j)),
                            Stroke = Brushes.Yellow,
                            StrokeThickness = 1
                        });
                    }
                }

                foreach (var line in tempList)
                {
                    retCanvas.Children.Add(line);
                }
            }

            return retCanvas;
        }

        private void UpdateWeapon(UpdateEventArgs e)
        {
            CurrentWeapon.Location = (Location - new Coordinate(Radius / 2, Radius / 2)) + (Coordinate)new PolarCoordinate(FaceDirection, 8);
            CurrentWeapon.Update(e);
        }

        public override void Collide(CollideEventArgs e)
        {
            if (Shield < 1)
            {
                if (OnDeath != null)
                    OnDeath(this, EventArgs.Empty);
            } else
            {
                Shield--;
            }
        }

        public void UpdateInput(bool[] keysPressed)
        {
            InputStatus = keysPressed;
        }

        public EventHandler OnDeath;
    }
}
